﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using BenefitHelper.Sys;
namespace BenefitHelper.Data.Day
{
    /// <summary>
    /// 经纪公司交易信息
    /// </summary>
    public class BrokerageDayTradeInfo
    {
        [Key]
        public int Id { get; set; }
        /// <summary>
        /// 操作账户
        /// </summary>
        public int BrokerId { get; set; }
        /// <summary>
        /// 操作账户
        /// </summary>
        public virtual Broker Broker { get; set; }
        /// <summary>
        /// 交易日
        /// </summary>
        public int TradeHistoryId { get; set; }
        /// <summary>
        /// 交易日
        /// </summary>
        public virtual TradeHistory TradeHistory { get; set; }

        /// <summary>
        /// 平仓盈亏
        /// </summary>
        public double Profit { get; set; }
        /// <summary>
        /// 手续费
        /// </summary>
        public double Free { get; set; }
        /// <summary>
        /// 净收益
        /// </summary>
        public double DayCount { get; set; }
        /// <summary>
        /// 交易笔数
        /// </summary>
        public double BillCount { get; set; }
        /// <summary>
        /// 总亏损
        /// </summary>
        public double SumLost { get; set; }
        /// <summary>
        /// 总盈利
        /// </summary>
        public double SumWin { get; set; }
        /// <summary>
        /// 保证金占用
        /// </summary>
        public double UsedMargin { get; set; }
        /// <summary>
        /// 盈利次数
        /// </summary>
        public int WinCount { get; set; }
        /// <summary>
        /// 亏损次数
        /// </summary>
        public int LoseCount { get; set; }

        /// <summary>
        /// 删除当日所有记录
        /// </summary>
        /// <param name="tradeHistoryId"></param>
        public void Delete(int tradeHistoryId, DB.DBManager db)
        {
            var query = from t in db.BrokerageDayTradeInfo where t.TradeHistoryId == tradeHistoryId select t;
            foreach (BrokerageDayTradeInfo d in query)
            {
                db.BrokerageDayTradeInfo.Remove(d);
            }
            db.SaveChanges();
        }


        /// <summary>
        /// 保存经纪公司日交易数据
        /// </summary>
        /// <param name="tradeHistoryId">插入时间编号</param>
        /// <param name="pdate">插入时间</param>
        public void InitBrokerDayTradeInfo(int tradeHistoryId, string pdate, DB.DBManager db)
        {
            var query = db.Broker.ToList();
            Data.ServerData.T_TradeDetail_History th = new ServerData.T_TradeDetail_History();
            foreach (Sys.Broker broker in query)
            {
                BrokerageDayTradeInfo brokerday = new BrokerageDayTradeInfo();
                brokerday.BillCount = th.GetBrokerDayBillCount(pdate, broker.BrokerId, db);
                brokerday.BrokerId = broker.Id;
                brokerday.DayCount = th.GetDayBrokerCloseProfit(pdate, broker.BrokerId, db) - th.GetDayBrokerCommission(pdate, broker.BrokerId, db);
                brokerday.Free = th.GetDayBrokerCommission(pdate, broker.BrokerId, db);
                brokerday.LoseCount = th.GetDayBrokerLoseCount(pdate, broker.BrokerId, db);
                brokerday.Profit = th.GetDayBrokerCloseProfit(pdate, broker.BrokerId, db);
                brokerday.SumLost = th.GetBrokerDayAllLose(pdate, broker.BrokerId, db);
                brokerday.SumWin = th.GetBrokerDayAllWin(pdate, broker.BrokerId, db);
                brokerday.TradeHistoryId = tradeHistoryId;
                brokerday.UsedMargin = th.GetDayBrokerMargin(pdate, broker.BrokerId, db);
                brokerday.WinCount = th.GetBrokerDayWinCount(pdate, broker.BrokerId, db);
                db.BrokerageDayTradeInfo.Add(brokerday);
            }
            db.SaveChanges();
        }
    }
}